package com.ctaa;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.common.pojo.app.OnboardingItem;
import com.common.utils.Constants;
import com.common.utils.ConstantsUrl;
import com.ctaa.adapter.OnboardingAdapter;
import com.ctaa.databinding.ActivityGuideBinding;
import com.ctaa.login.LoginActivity;

/**
 * 引导页
 */
public class GuideActivity extends AppCompatActivity {


    private ActivityGuideBinding guideBinding;

    private ViewPager2 viewPager;
    private LinearLayout indicatorContainer;
    private Button btnSkip, btnStart;
    private OnboardingAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        guideBinding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(guideBinding.getRoot());

        //如果已经登陆过了的就直接使用
        SharedPreferences preferences = getSharedPreferences("login", MODE_PRIVATE);
        String userInfo = preferences.getString("userInfo", null);
        if (userInfo != null) {
            Constants.userInfo = userInfo;
            startMainActivity();
            finish();
            return;
        }

        // 检查是否首次启动
        if (!isFirstLaunch()) {
            startLoginActivity();
            finish();
            return;
        }

        // 初始化视图
        initViews();

        // 设置引导页数据
        setupOnboardingItems();

        // 初始化指示器
        setupIndicators();

        // 设置页面变化监听
        setupPageChangeListener();

        // 设置按钮点击事件
        setupButtonListeners();
    }

    private void initViews() {
        viewPager = guideBinding.viewPager;
        indicatorContainer = guideBinding.indicatorContainer;
        btnSkip = guideBinding.btnSkip;
        btnStart = guideBinding.btnStart;
    }

    private void setupOnboardingItems() {
        java.util.List<OnboardingItem> items = new java.util.ArrayList<>();

        // 添加引导页内容
        items.add(new OnboardingItem(
                "欢迎使用我们的应用",
                "探索全新体验，发现更多精彩",
                R.drawable.onboarding1,
                false
        ));

        items.add(new OnboardingItem(
                "个性化推荐",
                "根据您的喜好，为您推荐专属内容",
                R.drawable.onboarding2,
                false
        ));

        items.add(new OnboardingItem(
                "实时互动",
                "与全球用户实时交流，分享生活点滴",
                R.drawable.onboarding3,
                true // 最后一页
        ));

        adapter = new OnboardingAdapter(items);
        viewPager.setAdapter(adapter);
    }

    private void setupIndicators() {
        int count = adapter.getItemCount();
        View[] indicators = new View[count];
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(8, 0, 8, 0);

        for (int i = 0; i < count; i++) {
            indicators[i] = new View(this);
            indicators[i].setLayoutParams(params);
            indicatorContainer.addView(indicators[i]);
        }
    }

    private void setupPageChangeListener() {
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // 最后一页显示开始按钮，隐藏跳过按钮
                if (position == adapter.getItemCount() - 1) {
                    btnSkip.setVisibility(View.GONE);
                    btnStart.setVisibility(View.VISIBLE);
                } else {
                    btnSkip.setVisibility(View.VISIBLE);
                    btnStart.setVisibility(View.GONE);
                }
            }
        });
    }


    private void setupButtonListeners() {
        btnSkip.setOnClickListener(v -> {
            setFirstLaunch(false);
            startLoginActivity();
            finish();
        });

        btnStart.setOnClickListener(v -> {
            setFirstLaunch(false);
            startLoginActivity();
            finish();
        });
    }

    private boolean isFirstLaunch() {
        SharedPreferences preferences = getSharedPreferences("onboarding", MODE_PRIVATE);
        return preferences.getBoolean("first_launch", true);
    }

    private void setFirstLaunch(boolean isFirst) {
        SharedPreferences preferences = getSharedPreferences("onboarding", MODE_PRIVATE);
        preferences.edit().putBoolean("first_launch", isFirst).apply();
    }

    private void startLoginActivity() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}